<template>
  <div class="reg-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="../../assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 注册的表单 -->
      <el-form :model="regForm" ref="regRef" :rules="regRules">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-user"
            v-model="regForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
            v-model="regForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请再次输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
            v-model="regForm.repassword"
          ></el-input>
        </el-form-item>
        <el-button @click="regname" type="primary" class="btn-login">注册</el-button>
        <el-link @click="$router.push('/login')" type="info" class="link-reg">去登录</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>
import { reqRegister } from '@/api/user'

export default {
  name: 'Reg',

  data () {
    const checkReg = (rule, value, cb) => {
      if (value !== this.regForm.password) {
        return cb(new Error('两次输入的密码必须一致'))
      }
      cb()
    }
    return {
      regForm: {
        usermane: '',
        password: '',
        repassword: ''
      },
      regRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['change', 'blur'] },
          { pattern: /^[0-9a-zA-Z]{5,10}$/, message: '用户名必须是5到10位数', trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '密码必须是6到15位数', trigger: ['change', 'blur'] }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '密码必须是6到15位数', trigger: ['change', 'blur'] },
          { validator: checkReg, trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    regname () {
      this.$refs.regRef.validate(async boo => {
        if (!boo) return null
        const { data: { code, message } } = await reqRegister(this.regForm)
        if (code !== 0) return this.$message.info(message)
        this.$message.info(message)
        this.$router.push('/login')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.reg-container {
  background-color: blue;
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
      background: url('../../assets/images/login_title.png') no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      font-size: 12px;
    }
  }
}
</style>
